import 'package:boss/color_constant.dart';
import 'package:boss/models/takeout_order_list_data.dart';
import 'package:boss/textStyle/text_style.dart';
import 'package:boss/ui/widget/image/base_image.dart';
import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';

class CommodityRow extends StatelessWidget {
  final TakeOutOrderDetails data;

  const CommodityRow({Key key, this.data}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    List<Widget> children = [
      Container(
        padding: EdgeInsets.symmetric(vertical: 10.w),
        child: Row(
          children: <Widget>[
            Expanded(
              child: Row(
                children: <Widget>[
                  Text(data.titleWithSku),
                  if (data.status == 1)
                    Container(
                      margin: EdgeInsets.only(
                        left: 10.w,
                      ),
                      transform: Matrix4.translationValues(0, -12.w, 0),
                      child: BaseImage(
                        'assets/image/takeout_back.png',
                        width: 24.w,
                        height: 24.w,
                      ),
                    ),
                ],
              ),
              flex: 4,
            ),
            Expanded(
              child: Text('x${data.num}'),
              flex: 1,
            ),
            Expanded(
              child: TakeOutPriceText(
                price: data.commodityActualFee,
              ),
            ),
          ],
        ),
      )
    ];

    if (data.takeOutOrderTastes.length > 0) {
      var tasteText = '';
      data.takeOutOrderTastes.forEach((item) {
        tasteText += item.tasteLabel;

        if (data.takeOutOrderTastes.last != item) {
          tasteText += ',';
        }
      });

      children.add(Container(
        padding: EdgeInsets.symmetric(vertical: 5.w),
        child: Text(
          '口味做法：$tasteText',
          style: TextStyleConstant().normal_3_24,
        ),
      ));
    }

    data.takeOutOrderMeterials?.forEach((item) {
      children.add(Container(
        padding: EdgeInsets.symmetric(vertical: 5.w),
        child: Row(
          children: <Widget>[
            Expanded(
              child: Text(
                item.meterialName,
                style: TextStyleConstant().normal_3_24,
              ),
              flex: 4,
            ),
            Expanded(
              child: Text(
                'x${item.unitNum}',
                style: TextStyleConstant().normal_3_28,
              ),
              flex: 1,
            ),
            Expanded(
              child: TakeOutPriceText(
                price: item.meterialInitialAmount,
              ),
            ),
          ],
        ),
      ));
    });

    return Container(
      padding: EdgeInsets.symmetric(
        vertical: 15.w,
        horizontal: 30.w,
      ),
      decoration: BoxDecoration(border: Border(bottom: BorderSide(color: ColorConstant.e6e6, width: 1.px))),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisAlignment: MainAxisAlignment.start,
        children: children,
      ),
    );
  }
}

class TakeOutPriceText extends StatelessWidget {
  final int price;
  final bool isBack;
  final Color color;

  const TakeOutPriceText({
    this.price = 0,
    this.isBack = false,
    this.color,
    Key key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    var price = this.price ?? 0;
    final yuan = price ~/ 100;
    final fen = price % 100;
    final color = this.color ?? (isBack ? ColorConstant.ff99 : ColorConstant.ff33);

    return RichText(
      textAlign: TextAlign.end,
      text: TextSpan(
          text: '¥',
          style: TextStyle(
            fontSize: 20.sp,
            color: color,
            fontWeight: FontWeight.bold,
          ),
          children: [
            TextSpan(
              text: '$yuan',
              style: TextStyle(
                fontSize: 36.sp,
                color: color,
                fontWeight: FontWeight.bold,
                fontFamily: 'DINAlternate-Bold',
              ),
            ),
            TextSpan(
              text: '.$fen',
              style: TextStyle(
                fontSize: 20.sp,
                color: color,
                fontWeight: FontWeight.bold,
                fontFamily: 'DINAlternate-Bold',
              ),
            ),
          ]),
    );
  }
}
